import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Layout from '../components/Layout.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/home',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('../views/Home.vue'),
        meta: { title: '首页' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue'),
        meta: { title: '个人信息' }
      },
      // 权限管理
      {
        path: 'permission',
        name: 'Permission',
        redirect: '/dashboard/permission/role',
        meta: { title: '权限管理' },
        children: [
          {
            path: 'role',
            name: 'RoleManagement',
            component: () => import('../views/permission/RoleManagement.vue'),
            meta: { title: '角色管理' }
          },
          {
            path: 'account',
            name: 'AccountManagement',
            component: () => import('../views/permission/AccountManagement.vue'),
            meta: { title: '账号管理' }
          }
        ]
      },
      // 组织管理
      {
        path: 'organization',
        name: 'Organization',
        redirect: '/dashboard/organization/department',
        meta: { title: '组织管理' },
        children: [
          {
            path: 'department',
            name: 'DepartmentManagement',
            component: () => import('../views/organization/DepartmentManagement.vue'),
            meta: { title: '部门管理' }
          }
        ]
      },
      // 团队测评
      {
        path: 'assessment',
        name: 'Assessment',
        redirect: '/dashboard/assessment/team',
        meta: { title: '团队测评' },
        children: [
          {
            path: 'team',
            name: 'TeamAssessment',
            component: () => import('../views/assessment/TeamAssessment.vue'),
            meta: { title: '项目团队' }
          },
          {
            path: 'team/:teamId/members',
            name: 'TeamMembers',
            component: () => import('../views/assessment/TeamMembers.vue'),
            meta: { title: '团队成员' }
          },
          {
            path: 'team/:teamId/reports',
            name: 'TeamReports',
            component: () => import('../views/assessment/TeamReports.vue'),
            meta: { title: '团队报告' }
          },
          {
            path: 'entry',
            name: 'AssessmentEntry',
            component: () => import('../views/assessment/AssessmentEntry.vue'),
            meta: { title: '测评入口' }
          },
          {
            path: 'report',
            name: 'AssessmentReport',
            component: () => import('../views/assessment/AssessmentReport.vue'),
            meta: { title: '查询报告入口' }
          }
        ]
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  if (to.path === '/login') {
    next()
  } else if (!token) {
    next('/login')
  } else {
    next()
  }
})

export default router
